package pl.szpadel.android.gadu;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.provider.Settings;

/// Wrapper for shared config.
/// Provides convenient access to cofig values and handles reading/writing
public class Config {
	
	private static final String TAG = "Config";
	
	////////////////
	// keys and other constants
	
	// config files
	static public final String FILE_NAME = "config";
	
	// config keys
	static public final String UIN = "uin"; // user GG number
	static public final String PASSWORD = "password"; // user password
	static public final String CONNECTED = "connected"; // connect automatically?
	static public final String STATUS = "status"; // status (online, away, busy, invisible)
	static public final String DESCRIPTION = "description";
	
	
	//////////////////
	// cached values
	private long mUin;
	private String mPassword = "";
	private Status mStatus = new Status(Status.GG_STATUS_AVAIL, "AndroidGadu");
	private boolean mConnected = false;
	
	
	//////////////////
	// getters/setters
	public long getUin() {
		return mUin;
	}
	public void setUin(long uin) {
		mUin = uin;
	}
	public String getPassword() {
		return mPassword;
	}
	public void setPassword(String password) {
		mPassword = password;
	}
	public Status getStatus() {
		return mStatus;
	}
	public void setStatus(Status status) {
		mStatus = status;
	}
	public boolean getConnected() {
		return mConnected;
	}
	public void setConnected(boolean connected) {
		mConnected = connected;
	}
	
	///////////////////////
	// Preferneces managed by UI
	
	public String getNotificationSound() {
		Context ctx = App.getInstance().getContext();
		SharedPreferences config = PreferenceManager.getDefaultSharedPreferences(ctx);
		return config.getString("notification_sound", Settings.System.DEFAULT_NOTIFICATION_URI.toString());
	}
	
	public boolean getUseVibration() {
		Context ctx = App.getInstance().getContext();
		SharedPreferences config = PreferenceManager.getDefaultSharedPreferences(ctx);
		return config.getBoolean("use_vibrations", true);
	}
	
	public boolean getUseLeds() {
		Context ctx = App.getInstance().getContext();
		SharedPreferences config = PreferenceManager.getDefaultSharedPreferences(ctx);
		return config.getBoolean("use_leds", true);
	}
	
	public boolean getShowStatusbarIcon() {
		Context ctx = App.getInstance().getContext();
		SharedPreferences config = PreferenceManager.getDefaultSharedPreferences(ctx);
		return config.getBoolean("show_statusbar_icon", true);
	}
	
	public boolean getStartOnBoot() {
		Context ctx = App.getInstance().getContext();
		SharedPreferences config = PreferenceManager.getDefaultSharedPreferences(ctx);
		return config.getBoolean("start_on_boot", true);
	}
	
	public boolean getLogToFile() {
		Context ctx = App.getInstance().getContext();
		SharedPreferences config = PreferenceManager.getDefaultSharedPreferences(ctx);
		return config.getBoolean("log_to_file", false);
	}

	public int getLogLevel() {
		Context ctx = App.getInstance().getContext();
		return getLogLevel(ctx);
	}
	
	public boolean getLogToFile(Context ctx) {
		SharedPreferences config = PreferenceManager.getDefaultSharedPreferences(ctx);
		return config.getBoolean("log_to_file", false);
	}

	public int getLogLevel(Context ctx) {
		SharedPreferences config = PreferenceManager.getDefaultSharedPreferences(ctx);
		String levelStr = config.getString("log_level", "3");
		try {
			return Integer.parseInt(levelStr);
		} catch(NumberFormatException e) {
			return 3;
		}
	}

	////////////////////////
	// Other
	
	/// Checks if config contains logion details
	public boolean hasLoginDetails() {
		return (mUin > 0 && mPassword.length() > 0);
	}
	
	////////////////////////
	// i/o
	
	/// Reads from shared config
	public void read(Context ctx) {
		try {
			//SharedPreferences config = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
			SharedPreferences config = PreferenceManager.getDefaultSharedPreferences(ctx);
			
			// simple fields
			mPassword = config.getString(PASSWORD, "");
			mUin = config.getLong(UIN, 0);
			mConnected = config.getBoolean(CONNECTED, false);
			
			// read status
			String status = config.getString(STATUS, "");
			String description = config.getString(DESCRIPTION, "AndroidGadu");
			
			mStatus = new Status(status, description);
			
			AGLog.i(TAG, "Successfuly read, uin=" + mUin);
		} catch (Exception e) {
			AGLog.e(TAG, "Error reading config: " + e);
		}
	}
	
	/// Wrties to config
	public void write(Context ctx) {
		try {
			//SharedPreferences config = ctx.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
			SharedPreferences config = PreferenceManager.getDefaultSharedPreferences(ctx);
			SharedPreferences.Editor editor = config.edit();
		
			editor.putLong(UIN, mUin);
			editor.putString(PASSWORD, mPassword);
			editor.putBoolean(CONNECTED, mConnected);
			editor.putString(DESCRIPTION, mStatus.getDescription());
			editor.putString(STATUS, mStatus.toConfigString());
			
			editor.commit();
			
			AGLog.i(TAG, "Successfully written, uin=" + mUin);
		} catch (Exception e) {
			AGLog.e(TAG, "Error writing config: " + e);
		}
	}
	
	
}
